import pymysql as pymysql
# 获取jdbc连接
def getConn():
    return pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='public_opinion_py', charset='utf8')
# 从数据库获取数据
def controlDB(sql):
    conn = getConn()
    conn.autocommit(1)
    cursor = conn.cursor();
    try:
        cursor.execute(sql)
        return cursor
    except:
        import traceback
        traceback.print_exc()
        # 发生错误时回滚
        conn.rollback()
    finally:
        # 关闭游标连接
        cursor.close()
        # 关闭数据库连接
        conn.close()
